package middleware

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/gomodule/redigo/redis"
	cache2 "wx-api/dsp-api/cache"
	config2 "wx-api/dsp-api/config"
)

func CheckSspLogin() func(c *gin.Context) {
	return func(c *gin.Context) {
		token := c.GetHeader("token")
		if token == "" {
			c.JSON(200, gin.H{
				"status": -4,
				"msg":    "Login auth token is empty",
			})
			c.Abort()
			return
		}
		//获取缓存
		conn := config2.RedisDb.Get()
		defer conn.Close()
		tmpid, err := redis.String(conn.Do("GET", "dsp_login-"+token))
		if tmpid == "" {
			fmt.Println("Login auth token is error 002", "login-"+token)
			if err != nil {
				fmt.Println("redis ck login error:", err.Error())
			}
			c.JSON(200, gin.H{
				"status": -4,
				"msg":    "Login auth token is error 002",
			})
			c.Abort()
			return
		}
		//获取用户登陆信息
		info, ok := cache2.CacheDspUserMapNGet(tmpid)
		if !ok {
			c.JSON(401, gin.H{
				"status": -4,
				"msg":    "Login auth is fail",
			})
			c.Abort()
			return
		}
		c.Set("dspuser", info.Id.Hex())
		c.Next()
		return
	}
}
